import { useSelector,useDispatch } from "react-redux";
import { useCallback } from "react";
import ordersApi from "../api/orders.js";
// import * as Creators from "../store/actions";
import Creators from "../store/actions.js";

export const useOrders = () =>{

    const orders = useSelector(state =>state.orders);
    const dispatch = useDispatch();
    const {curPage,eachPage} = orders;

    // console.log(curPage,eachPage);

    const getOrders = useCallback( async()=>{
        
        const data = await ordersApi.get({curPage,eachPage});
        dispatch(Creators.getOrders(data));
    },[curPage,eachPage,dispatch]);
    
    const setCurPage = useCallback((curPage)=>{
        
        dispatch(Creators.setCurPage({curPage}))
    },[dispatch]);

    const setEachPage = useCallback((eachPage)=>{
        orders.rows=[];
        dispatch(Creators.setEachPage({eachPage}))
    },[dispatch]);
 
    return [orders,{getOrders,setCurPage,setEachPage}]
}